'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 市场
 * @author sundbcn@gmail.com (sun debing)
 */
var _ = require('lodash');
var dataApi = require('../libs/dataApi');

/**
 * 获取市场物品概率信息
 * @return {[type]}     [description]
 */
exports.getMarketRateInfo = function () {
    var market_lvl_conf = {};
    dataApi.market.any(function (market_lv_arr) {
        var market_lv_info = market_lv_arr[0];
        var lvl = market_lv_info.id;
        market_lvl_conf[lvl] = {
            lvl: market_lv_info.id,
            total_prob: 0,
            market_rate: []
        };
        var market_prob = [];
        _.some(market_lv_arr, function (market_cf_item) {
            market_prob.push({
                market_id: market_cf_item.goods_id,
                probability: market_cf_item.probability
            });
        });

        var total_prob = 0;
        var market_rate = [];
        _.each(market_prob, function (rate_item) {
            total_prob += rate_item.probability;

            market_rate.push({
                market_id: rate_item.market_id,
                sum_rate: total_prob
            });
        });
        market_lvl_conf[lvl].total_prob = total_prob;
        market_lvl_conf[lvl].market_rate = market_rate;
    });
    return market_lvl_conf;
};

/**
 * 获取市场物品信息
 * @param  {[type]} market_id [description]
 * @return {[type]}     [description]
 */
exports.findByID = function (lvl, market_id) {
    var market_lvl_conf = dataApi.market.findById(lvl);
    return _.find(market_lvl_conf, {
        goods_id: market_id
    });
};

/**
 * 获取市场折扣信息数组
 * @return {[type]}     [description]
 */
exports.getMarketDiscountArray = function () {
    var disc_array = [];
    dataApi.market_discount.any(function (disc_info, key) {
        disc_array.push(disc_info);
    });
    return disc_array;
};
